package com.lzj.task;

import com.lzj.model.Orders;
import lombok.Getter;
import lombok.ToString;
import org.jetbrains.annotations.NotNull;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

@Getter
@ToString
public class Task<T> implements Delayed {

    private final T order;

    private final long expire;
    /**
     * 任务
     *
     * @param order  次序
     * @param expire 到期时间
     */
    public Task(T order, long expire){
        super();
        this.order = order;
        this.expire = expire;
    }

    @Override
    public long getDelay(@NotNull TimeUnit timeUnit) {
        return timeUnit.convert(this.expire - System.currentTimeMillis(), TimeUnit.NANOSECONDS);
    }

    @Override
    public int compareTo(@NotNull Delayed delayed) {
        return (int) (getDelay(TimeUnit.NANOSECONDS) - delayed.getDelay(TimeUnit.NANOSECONDS));
    }
}
